package com.songlanyun.modules.opus.controller.admin;

import com.songlanyun.common.annotation.SysLog;
import com.songlanyun.common.enums.PrivateMessageConstant;
import com.songlanyun.common.enums.SysConstant;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;
import com.songlanyun.modules.exception.BbsException;
import com.songlanyun.modules.message.model.to.MessageTO;
import com.songlanyun.modules.message.service.SendPrivateMessageMsgService;
import com.songlanyun.modules.opus.entity.Opus;
import com.songlanyun.modules.opus.service.OpusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;


/**
 * 作品表
 *
 * @author lubo
 * @email sunlightcs@gmail.com
 * @date 2021-01-13 16:35:02
 */
@RestController
@RequestMapping("admin/opus")
public class OpusAdminController {
    @Autowired
    private OpusService opusService;

    @Autowired
    private SendPrivateMessageMsgService sendPrivateMessageMsgService;

    /**
     * 列表
     */
    @GetMapping("/list")
    //@RequiresPermissions("opus:opus:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = opusService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 作品详情
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions("opus:opus:info")
    public R info(@PathVariable("id") Long id) {
        Opus opus = opusService.adminInfo(id);

        return R.ok().put("opus", opus);
    }

    /**
     * 审核
     */
    @SysLog("审核作品")
    @PostMapping("/audit")
    //@RequiresPermissions("opus:opus:save")
    public R audit(@RequestBody Opus opus) {
        if (opus.getId() == null || opus.getOpusStatus() == null) {
            throw new RRException(BbsException.OPUS_AUDIT_PARAMETER_PARTIAL);
        }
        Opus audit = opusService.audit(opus);

        //发送互动消息
        sendPrivateMessageMsgService.send(new MessageTO(PrivateMessageConstant.MessageType.INTERACTION_MESSAGE,
                PrivateMessageConstant.ModelType.CHECK, audit.getId(),
                SysConstant.InteractionType.OPUS_CHECK, audit.getPublisherId()));
        return R.ok();
    }

    /**
     * 置顶
     */
    @SysLog("置顶作品")
    @PostMapping("/stick")
    //@RequiresPermissions("opus:opus:save")
    public R stick(@RequestBody Opus opus) {
        if (opus.getId() == null || opus.getOpenStick() == null) {
            throw new RRException(BbsException.OPUS_STICK_PARAMETER_PARTIAL);
        }
        opusService.stick(opus);

        return R.ok();
    }

//    /**
//     * 删除
//     */
//    @DeleteMapping("/delete/{id}")
//    //@RequiresPermissions("opus:opus:delete")
//    public R delete(@PathVariable("id") Long id) {
//        opusService.deleteById(id);
//        return R.ok();
//    }

}
